package com.dykj.fireFighting.web.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jxls.builder.xls.XlsCommentAreaBuilder;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author:zhujunming
 * @Date: 2019/6/24 8:19
 * @For: 文档导出
 **/
public class ExportUtils {
    static{
        XlsCommentAreaBuilder.addCommandMapping("merge", MergeCommand.class);
    }
    public static void exportExcel(Map<String,Object> map, String modelName, String returnName, HttpServletResponse response)throws Exception
    {
        Context context=new Context();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String mapKey = entry.getKey();
            Object mapValue = entry.getValue();
            context.putVar(mapKey,mapValue);
        }
        InputStream is= Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplate/"+modelName);
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        JxlsHelper.getInstance().setUseFastFormulaProcessor(false).processTemplate(is, os, context);
        response.setContentType("application/vnd.ms-excel");
        String name = java.net.URLEncoder.encode(returnName, "UTF-8");
        response.setHeader("Content-disposition", "filename=\"" + name + "\"");
        // 获得response中的输出流
        ServletOutputStream sout = response.getOutputStream();

        os.writeTo(sout);
        os.close();
        sout.flush();
        sout.close();
        // mon

    }

    public static XSSFWorkbook drawPic(InputStream imgInput, XSSFWorkbook workBook, int chushix, int chushiy) throws Exception{
        /* 改excel模板的时候 不要随意拉动单元格长度 否则图片会受到影响*/
        int morenchang=72;//单元格长度
        int morenkuan=18;
        XSSFSheet sheet =workBook.getSheetAt(0);
        XSSFDrawing patriarch = sheet.createDrawingPatriarch();

        BufferedImage bufferImg= ImageIO.read(imgInput);
        int chang=(bufferImg.getWidth()/morenchang)+((bufferImg.getWidth()%morenchang>(morenchang/2)?1:0));
        int kuan=(bufferImg.getHeight()/morenkuan)+((bufferImg.getHeight()%morenkuan>(morenkuan/2)?1:0));

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        ImageIO.write(bufferImg, "png", byteArrayOut);
        XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,
                (short) chushix, chushiy, (short) (chushix+chang), (chushiy+kuan));
        chushiy=chushiy+kuan;
        patriarch.createPicture(anchor, workBook.addPicture(byteArrayOut
                .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
        byteArrayOut.flush();
        byteArrayOut.close();
        return workBook;
    }
}
